package com.base.api;

import com.alibaba.fastjson.JSONObject;
import com.base.config.WXConfig;
import com.base.core.model.ApiRes;
import com.base.core.constant.Enums;
import com.base.core.utils.HttpClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * @Description
 * @Author: LY
 * @Date: 2020/11/30
 */
@Api(tags = "微信接口")
@RestController
@RequestMapping("/wx")
public class WXController {

    @ApiOperation(value = "获取微信分享签名")
    @PostMapping(value = "/obtainSignature")
    public ApiRes obtainSignature(@RequestBody JSONObject param) throws IOException, ParseException {


        if (Objects.isNull(param) || StringUtils.isBlank(param.getString("url"))) {
            return ApiRes.failed(Enums.ResponseStatus.MISS_DATA);
        }


        Map paramWX = new HashMap(16);

        paramWX.put("grant_type", "client_credential");
        paramWX.put("appid", WXConfig.APP_ID);
        paramWX.put("secret", WXConfig.APP_SECRET);


        HttpClient httpClient = new HttpClient("https://api.weixin.qq.com/cgi-bin/token", paramWX);

        httpClient.setHttps(true);

        httpClient.get();

        JSONObject json = httpClient.getContent();

        Integer errcode = json.getInteger("errcode");

        if (Objects.nonNull(errcode) && errcode != 0) {
            return ApiRes.failed(json.getString("errmsg"));
        }

        JSONObject access_token = getTicket(json.getString("access_token"));

        //时间戳
        long timestamp = System.currentTimeMillis();

        //随机字符串
        String noncestr = UUID.randomUUID().toString().replaceAll("-", "");

        //微信 ticket
        String ticket = access_token.getString("ticket");

        //网页url
        String url = param.getString("url");

        String str = new StringBuilder().append("jsapi_ticket=").append(ticket).append("&noncestr=").append(noncestr).append("&timestamp=").append(timestamp).append("&url=").append(url).toString();

        //签名字符串
        String signature = DigestUtils.sha1Hex(str);

        JSONObject resultParam = new JSONObject();

        resultParam.put("timestamp", timestamp);
        resultParam.put("noncestr", noncestr);
        resultParam.put("signature", signature);

        return ApiRes.success(resultParam);


    }

    /**
     * 获取ticket
     *
     * @param access_token
     * @return
     * @throws IOException
     * @throws ParseException
     */
    private JSONObject getTicket(String access_token) throws IOException, ParseException {

        Map param2 = new HashMap();
        param2.put("access_token", access_token);
        param2.put("type", "jsapi");

        HttpClient httpClient2 = new HttpClient("https://api.weixin.qq.com/cgi-bin/ticket/getticket", param2);
        httpClient2.setHttps(true);
        httpClient2.get();
        return httpClient2.getContent();
    }
}
